package operation;


import JDBC.JDBCUtils;
import Medicine.Pharmacy;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class DelEquipment implements IOPeration{
    @Override
    public void work(Pharmacy pharmacy) throws SQLException {
        //创建QueryRunner对象
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource() );
        System.out.println(" 删除器械.... ");
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入您想要删除的器械");
        String name = sc.nextLine();

        String query = "SELECT Name FROM Equipment";
        int flag = 0;
        List<Map<String, Object>> equipmentList = qr.query(query, new MapListHandler());
        for (Map<String, Object> equipmentMap : equipmentList) {
            if (equipmentMap.get("name").equals(name)) {
                System.out.println("有你需要删除的器械");
                flag = 1;
                break;
            }
        }
        if(flag == 0){
            System.out.println("没有您想删除的器械");
            return;
        }

        //定义根据名字删除器材的SQL
        String sql = "DELETE FROM Equipment WHERE name=?";
        int count = qr.update(sql,name);
        //输出删除结果
        if(count<1){
            System.out.println("删除器械失败！");
        }else{
            System.out.println("删除器械成功！");
        }

    }
}

        /*int index = -1;
        int i = 0;
        int currSize = pharmacy.getUsedSize();*/
        /*for (i = 0; i < currSize; i++) {
            // 1
            Goods book = pharmacy.getMedicines(i);
            if (book.getName().equals(name)){
                index = i;
                break;
            }
        }*/
        /*if (i>currSize){
            System.out.println("没有你想要删除的书");
            return;
        }

        for (int j = index; j < currSize-1; j++) {
            Goods temp = pharmacy.getMedicines(j+1);
            pharmacy.setMedicines(j,temp);
        }

        pharmacy.setUsedSize(currSize-1);

        pharmacy.setMedicines(currSize-1,null);

        System.out.println("删除图书成功了！！");*/
